﻿using Sidvall.Data;
using Sidvall.LogManager.Net.Services.V1;
using System.Linq;

namespace Sidvall.LogManager
{
    public static class ParameterManager
    {
        public const string ServerIdParameterName = "ServerId";
        public const string ServerConnectionParameterName = "ServerConnection";
        public const string GetEventItemsCriteriaId = "GetEventItems";
        public const string DeleteEventItemsCriteriaId = "DeleteEventItems";

        #region GetServerConnection

        public static ServerConnection GetServerConnection(ParameterCollection parameters)
        {
            if (parameters == null)
                return null;

            // Find ServerConnection
            var serverConnection = parameters.ValueToString(ParameterManager.ServerConnectionParameterName, null);
            var item = ServerConnection.Deserialize(serverConnection);
            if (item != null)
                return item;

            // Find ServerId
            if (Sidvall.LogManager.SystemContext.Current.UserConfiguration?.ServerConnections == null)
                return null;
            if (Sidvall.LogManager.SystemContext.Current.UserConfiguration.ServerConnections.Items.Count == 1)
            {
                return Sidvall.LogManager.SystemContext.Current.UserConfiguration.ServerConnections.Items[0];
            }
            else
            {
                var serverConnectionId = parameters.ValueToNullableInt32(ParameterManager.ServerIdParameterName);
                return GetServerConnection(serverConnectionId);
            }
        }
        public static ServerConnection GetServerConnection(int? serverConnectionId)
        {
            if (serverConnectionId == null)
                return null;
            if (Sidvall.LogManager.SystemContext.Current.UserConfiguration?.ServerConnections?.Items == null)
                return null;
            return (from o in Sidvall.LogManager.SystemContext.Current.UserConfiguration.ServerConnections.Items
                    where o.ServerConnectionId == serverConnectionId.Value
                    select o).FirstOrDefault();
        }

        #endregion

        #region GetLogScopeFetchListCriteria

        public static FetchListCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeField, global::Sidvall.LogManager.Data.Relations.LogScopeRelation> GetLogScopeFetchListCriteria(int serverConnectionId)
        {
            FetchListCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeField, global::Sidvall.LogManager.Data.Relations.LogScopeRelation> item;

            item = new FetchListCriteria<global::Sidvall.LogManager.Data.Fields.LogScopeField, global::Sidvall.LogManager.Data.Relations.LogScopeRelation>();
            item.SortItems = new Sidvall.Data.SortItemCollection<Data.Fields.LogScopeField>();
            item.SortItems.Add(Data.Fields.LogScopeField.Name, false);
            InitializeParameters(item, serverConnectionId);

            return item;
        }

        #endregion

        #region InitializeParameters

        public static void InitializeParameters(ISaveContext context, int? serverConnectionId)
        {
            if (context == null)
                return;
            if (context.Settings == null)
            {
                if (serverConnectionId != null)
                    context.Settings = new SaveSettings();
            }
            InitializeParameters(context.Settings, serverConnectionId);
        }
        public static void InitializeParameters(CriteriaBase criteria, int? serverConnectionId)
        {
            if (criteria == null)
                return;
            if (criteria.Parameters == null)
            {
                if (serverConnectionId != null)
                    criteria.Parameters = new ParameterCollection();
            }
            InitializeParameters(criteria.Parameters, serverConnectionId);
        }
        public static void InitializeParameters(ParameterCollection parameters, int? serverConnectionId)
        {
            if (parameters == null)
                return;
            parameters.Remove(ServerIdParameterName);
            if (serverConnectionId != null)
                parameters.Add(ServerIdParameterName, serverConnectionId.Value);
        }

        public static void InitializeParameters(ISaveContext context, ServerConnection serverConnection)
        {
            if (context == null)
                return;
            if (context.Settings == null)
            {
                if (serverConnection != null)
                    context.Settings = new SaveSettings();
            }
            InitializeParameters(context.Settings, serverConnection);
        }
        public static void InitializeParameters(CriteriaBase criteria, ServerConnection serverConnection)
        {
            if (criteria == null)
                return;
            if (criteria.Parameters == null)
            {
                if (serverConnection != null)
                    criteria.Parameters = new ParameterCollection();
            }
            InitializeParameters(criteria.Parameters, serverConnection);
        }
        public static void InitializeParameters(ParameterCollection parameters, ServerConnection serverConnection)
        {
            if (parameters == null)
                return;
            parameters.Remove(ServerConnectionParameterName);
            if (serverConnection != null)
                parameters.Add(ServerConnectionParameterName, serverConnection.Serialize());
        }

        #endregion
    }
}
